/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.www.actionforms.nodeactionforms;

import com.velocityme.entity.Activity;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.TaskStateMachineLocal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author rcrida
 */
public class ActivityActionForm extends TaskActionForm {

    private long m_taskTypeId;
    private byte m_percentageComplete = 0;
    private int m_durationDays = 0;
    private int m_durationHours = 0;
    private int m_durationMins = 0;

    public void extractActivity(KeyLocal key, Activity activity) {
        this.extractTask(key, activity);
        activity.setTaskStateMachine(lookupTaskStateMachineBean().findTaskStateMachine(key, m_taskTypeId));
        activity.setPercentageComplete(m_percentageComplete);
        activity.setExpectedDuration_min(getDurationTotalMins());
    }

    public long getTaskTypeId() {
        return m_taskTypeId;
    }

    public void setTaskTypeId(long taskTypeId) {
        m_taskTypeId = taskTypeId;
    }

    public byte getPercentageComplete() {
        return m_percentageComplete;
    }

    public void setPercentageComplete(byte percentageComplete) {
        m_percentageComplete = percentageComplete;
    }

    public int getDurationDays() {
        return m_durationDays;
    }

    public void setDurationDays(int durationDays) {
        m_durationDays = durationDays;
    }

    public int getDurationHours() {
        return m_durationHours;
    }

    public void setDurationHours(int durationHours) {
        m_durationHours = durationHours;
    }

    public int getDurationMins() {
        return m_durationMins;
    }

    public void setDurationMins(int durationMins) {
        m_durationMins = durationMins;
    }

    private int getDurationTotalMins() {
        return m_durationDays * 8 * 60 + m_durationHours * 60 + m_durationMins;
    }

    public void setDurationTotalMins(int totalMins) {
        m_durationDays = totalMins / (8 * 60);
        m_durationHours = (totalMins / 60) % 8;
        m_durationMins = totalMins % 60;
    }

    private TaskStateMachineLocal lookupTaskStateMachineBean() {
        try {
            Context c = new InitialContext();
            return (TaskStateMachineLocal) c.lookup("java:comp/env/TaskStateMachineBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
